﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="BoundingBox.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.Modifier.twoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using starLiGHT.ParticleSystem.Common;
    using starLiGHT.ParticleSystem.twoD;

    #endregion

    /// <summary>
    /// The BoundingBox modifier confines each particle to a rectangular region. The 
    /// particle bounces back off the sides of the rectangle when it reaches the edge.
    /// The bound treats the particle as a circular body. By default, no energy is lost 
    /// during collision. This can be modified by setting the damp property to a value 
    /// other than 1.0f (the default).
    /// </summary>
    /// <remarks>
    /// This modifier has a priority of -20, so that it executes after all movement has occured.
    /// </remarks>
    public class BoundingBox : Modifier2DBase
    {
        #region Member Variables
        private RectangleF bb;
        private float _damp;

        #endregion

        #region Constructors

        /// <summary>
        /// The constructor creates a BoundingBox modifier to be used by an emitter.
        /// </summary>
        public BoundingBox()
            : this(RectangleF.Empty, 1.0f)
        {
        }

        /// <summary>
        /// The constructor creates a BoundingBox modifier to be used by an emitter.
        /// </summary>
        /// <param name="rect">
        /// This represents the BoundingBox that is used for this modifier.
        /// </param>
        public BoundingBox(RectangleF rect)
            : this(rect, 1.0f)
        {
        }

        /// <summary>
        /// The constructor creates a BoundingBox modifier to be used by an emitter.
        /// </summary>
        /// <param name="rect">
        /// This represents the BoundingBox that is used for this modifier.
        /// </param>
        /// <param name="damp">
        /// The coefficient of restitution when the particles bounce off the sides of 
        /// the box. A value of 1.0f gives a pure elastic collision, with no energy loss. 
        /// A value between 0.0f and 1.0f causes the particle to loose energy in case of a 
        /// collision. A value greater than 1.0f causes the particle to gain energy in the 
        /// collision.
        /// </param>
        public BoundingBox(RectangleF rect, float damp)
        {
            Priority = -20;
            bb = rect;
            this._damp = damp;
        }
        #endregion

        /// <summary>
        /// This represents the BoundingBox that is used for this modifier.
        /// </summary>
        public RectangleF Bounds
        {
            get { return bb; }
            set { bb = value; }
        }

        /// <summary>
        /// The coefficient of restitution when the particles bounce off the sides of 
        /// the box. A value of 1.0f gives a pure elastic collision, with no energy loss. 
        /// A value between 0.0f and 1.0f causes the particle to loose energy in case of a 
        /// collision. A value greater than 1.0f causes the particle to gain energy in the 
        /// collision.
        /// </summary>
        public float Damp
        {
            get { return this._damp; }
            set { this._damp = value; }
        }

        /// <summary>
        /// Applies the bounce forces to a particle.
        /// </summary>
        /// <remarks>
        /// This method is called by the emitter and need not be called by the user.
        /// </remarks>
        /// <param name="emitter">The Emitter that created the particle.</param>
        /// <param name="particle">The particle to update.</param>
        /// <param name="time">The time since the last update.</param>
        public override void Update(Emitter2D emitter, Particle2D particle, float time)
        {
            float radius = particle.collisionRadius;
            float position;
            
            if (particle.velocity.X > 0 && (position = particle.position.X + radius) >= bb.Right)
            {
                particle.velocity.X = -particle.velocity.X * this._damp;
                particle.position.X += 2 * (bb.Right - position);
                ////////TODO: dispatch event Bounding_box_Collision
            }
            else if (particle.velocity.X < 0 && (position = particle.position.X - radius) <= bb.Left)
            {
                particle.velocity.X = -particle.velocity.X * this._damp;
                particle.position.X += 2 * (bb.Left - position);
                ////////TODO: dispatch event Bounding_box_Collision
            }

            if (particle.velocity.Y > 0 && (position = particle.position.Y + radius) >= bb.Bottom)
            {
                particle.velocity.Y = -particle.velocity.Y * this._damp;
                particle.position.Y += 2 * (bb.Bottom - position);
                ////////TODO: dispatch event Bounding_box_Collision
            }
            else if (particle.velocity.Y < 0 && (position = particle.position.Y - radius) <= bb.Top)
            {
                particle.velocity.Y = -particle.velocity.Y * this._damp;
                particle.position.Y += 2 * (bb.Top - position);
                ////////TODO: dispatch event Bounding_box_Collision
            }
        }
    }
}
